package org.ikongfuzi;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.ikongfuzi.common.PMF;
import org.ikongfuzi.common.Rssinfo;

import com.google.appengine.api.datastore.Link;
import com.sun.syndication.io.FeedException;

public class ReaderRSS {

	public ReaderRSS() {

	}

	/**
	 * 
	 * 描述:
	 * 
	 * @param map
	 * @return
	 * @author kelch 2010-11-19
	 */
	public Long insertRssinfo(Map map) {
		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			String str = (String) map.get("url");
			Link url = new Link(str.trim());
			String rssName = (String) map.get("rssName");
			rssName = rssName.trim();
			Rssinfo rss = new Rssinfo(rssName, url);
			List<Rssinfo> rInfo = checkRssinfoByName(rssName, url);
			if (rInfo == null || rInfo.size() < 1) {
				pm.makePersistent(rss);
				return rss.getId();// 成功
			}
			rss = rInfo.get(0);
			return rss.getId();// 存在
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return new Long(-1);
	}

	public int deleteById(Long id) {

		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
//			List<Rssinfo> rssinfos = null;
			Rssinfo rss = pm.getObjectById(Rssinfo.class,id);
			pm.deletePersistent(rss);
			return 0;//成功
//			Query query = pm.newQuery(Rssinfo.class);
			// query.setResultClass(Rssinfo.class);
//			rssinfos = (List<Rssinfo>) query.execute();
//			if (rssinfos != null)
//				rssinfos.size();
//			Iterator<Rssinfo> it = rssinfos.iterator();
//			while (it.hasNext()) {
//				Rssinfo r = it.next();
//				pm.deletePersistent(r);
//
//			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return -1;
	}
	/**
	 * 
	 * 描述:推荐
	 * @param id
	 * @return
	 * @author kelch
	 * 2011-9-22
	 */
	public int recommendById(Long id) {

		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Rssinfo rss = pm.getObjectById(Rssinfo.class,id);
			rss.setDefaultFlag("true");
			//pm.deletePersistent(rss);
			return 0;//成功
//			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return -1;
	}

	public List<Rssinfo> getRssinfoByName() {
		PersistenceManager pm = null;
		List<Rssinfo> rssinfos = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery(Rssinfo.class);
			// query.setResultClass(Rssinfo.class);
			rssinfos = (List<Rssinfo>) query.execute();
//			if (rssinfos != null)
//				rssinfos.size();// appEngine的bug，需要操作下返回结果。
//			Rssinfo rss = null;
//			for(int i=0;i<rssinfos.size();i++){
//				rss =rssinfos.get(i);
//				rss.setDefaultFlag("true");
//			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return rssinfos;
	}
	
	public List<Rssinfo> getRssinfoByDef(String def) {
		PersistenceManager pm = null;
		List<Rssinfo> rssinfos = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery(Rssinfo.class);
			query.setFilter("defaultFlag==def");
			query.declareParameters("String def");
			// query.setResultClass(Rssinfo.class);
			rssinfos = (List<Rssinfo>) query.execute(def);
			if (rssinfos != null)
				rssinfos.size();// appEngine的bug，需要操作下返回结果。
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return rssinfos;
	}

	public List<Rssinfo> checkRssinfoByName(String name, Link url) {
		PersistenceManager pm = null;
		List<Rssinfo> rssinfos = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery(Rssinfo.class);
			query.setFilter("rssName==namePara && url==urlPara");
			query.declareParameters("String namePara,com.google.appengine.api.datastore.Link urlPara");
			// Query query =
			// pm.newQuery("select from org.ikongfuzi.common.Rssinfo where rssName==name parameters String name");
			// Query query = pm.newQuery(Rssinfo.class,"rssName==name");
			// query.declareParameters("String name");
			// query.setFilter("rssName==namePara && url==urlPara");
			// query.declareImports("java.lang.String;import com.google.appengine.api.datastore.Link");
			// query.declareParameters("com.google.appengine.api.datastore.Link urlPara");
			// query.deletePersistentAll(arg0)
			Map map = new HashMap();
			map.put("namePara", name);
			map.put("urlPara", url);
			rssinfos = (List<Rssinfo>) query.executeWithMap(map);
			if (rssinfos != null)
				rssinfos.size();// appEngine的bug，需要操作下返回结果。
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		return rssinfos;
	}

	/**
	 * @param args
	 * @throws IOException
	 * @throws FeedException
	 * @throws IllegalArgumentException
	 */
	public static void main(String[] args) throws IllegalArgumentException,
			FeedException, IOException {
		PersistenceManager pm = null;
		try {
			// URL feedUrl = new
			// URL("http://rss.sina.com.cn/news/marquee/ddt.xml");
			//
			// SyndFeedInput input = new SyndFeedInput();
			// SyndFeed feed = input.build(new XmlReader(feedUrl));
			// //Writer writer = new FileWriter("./test.xml");
			// feed.setFeedType("rss_2.0"); // �趨feed����ĸ�ʽ
			// SyndFeedOutput output = new SyndFeedOutput();
			// List ls = feed.getEntries();
			// Iterator ir = ls.iterator();
			// SyndEntry ets = null;
			// SyndContent desc = null;
			// System.out.println("channel:"+feed.getTitle());
			// while(ir.hasNext()){
			// ets = (SyndEntry)ir.next();
			// desc = ets.getDescription();
			// System.out.println("item-title:"+ets.getTitle());
			// System.out.println("desc:"+desc.getValue());
			// System.out.println("link:"+ets.getLink());
			// }
			// System.out.print(output.outputString(feed));
			// writer.close();

			// List lis = new ReaderRSS().checkRssinfoByName("数码行情",new
			// Link("http://rss.enet.com.cn/rss/edigi/all.xml"));
			//new ReaderRSS().deleteAll();
			// pm = PMF.get().getPersistenceManager();
			// for(int i=0;i<lis.size();i++){
			// pm.deletePersistent(lis.get(i));
			//
			// }
			// pm.newQuery("delete from Rssinfo").execute();
			List<Rssinfo> rssinfos = null;
			try {
				pm = PMF.get().getPersistenceManager();
				Query query = pm.newQuery(Rssinfo.class);
				// query.setResultClass(Rssinfo.class);
				rssinfos = (List<Rssinfo>) query.execute();
				Rssinfo rss = null;
				for(int i=0;i<rssinfos.size();i++){
					rss =rssinfos.get(i);
					rss.setDefaultFlag("true");
				}
					
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				pm.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// pm.close();
		}

	}

}
